﻿using System.Linq;
using FarmerJoe.ClassSettings;
using FarmerJoe.Helpers;
using FarmerJoe.Helpers.Units;
using FarmerJoe.Settings;
using Styx;
using Styx.TreeSharp;

namespace FarmerJoe.Classes
{
    public static partial class Druid
    {
        public static Composite CombatBehavior_Guardian
        {
            get
            {
                return new PrioritySelector(

                    Spells.WaitForCast(),
                    Misc.EnsureTarget(),

                    //new Common.NeedToUseLifeSpirit(new Common.UseLifeSpirit()),
                    Common.UseHealthManaRegenAgent(ClassParser.HealthPotion, 0),
                    AllFormsHeal(),
                    
                    Inventory.UseTrinket1(),
                    Inventory.UseTrinket2(),

                    Spells.Cast("Bear Form", ret => Me.Shapeshift != ShapeshiftForm.Bear),
                    Movement.MoveToLOS(),
                    Movement.FaceTarget(),

                    Spells.Cast("Skull Bash", ret => Me.GotTarget && CT.IsCasting && CT.CurrentCastTimeLeft.TotalSeconds > 1 && CT.Distance2DSqr < 13 * 13 && CLC.ResultOK(FpswareSettings.Instance.Druid.GuardianSkullBash) && Timers.SpellOkToCast("Silence Bitch",3000)),
                    Spells.Cast("Mighty Bash", ret => CLC.ResultOK(FpswareSettings.Instance.Druid.GuardianMightyBash) && CT.IsWithinMeleeRange && Timers.SpellOkToCast("Silence Bitch", 3000)),
                    Spells.CastOnGround("Force of Nature", ret => CT.Location, ret => CLC.ResultOK(FpswareSettings.Instance.Druid.GuardianTrents)),

                    // Taunt that shit! 
                    Spells.Cast("Growl", ret => FpswareSettings.Instance.Druid.Taunt.Contains("always"), ret => Tanking.Instance.NeedToTaunt.FirstOrDefault()),

                    // Some AoE if we're in a party just to get group aggro
                    new Decorator(ret => Me.GroupInfo.IsInParty && Group.MeIsTank && CT.HostileUnitsNearTarget(8f).Count() >= 2,
                        new PrioritySelector(
                            Spells.Cast("Thrash", ret => ((FpswareSettings.Instance.Druid.GuardianThrash.Contains("adds") && CT.HostileUnitsNearTarget(8f).Count() >= 2) || FpswareSettings.Instance.Druid.GuardianThrash.Contains("always"))),
                            Spells.Cast("Swipe", ret => CLC.ResultOK(FpswareSettings.Instance.Druid.GuardianSwipe) && Timers.SpellOkToCast("Guardian Swipe",3000))
                            //new Action(ret=> Timers.Reset("Guardian Swipe"))

                            )),

                    Spells.Cast("Enrage", ret => Me.CurrentRage < FpswareSettings.Instance.Druid.EnrageRage),
                    
                    Spells.Cast("Wild Charge", ret => CLC.ResultOK(FpswareSettings.Instance.Druid.GuardianWildCharge) && CT.Distance>8 && CT.Distance<25),
                    Spells.Cast("Berserk", ret =>  CLC.ResultOK(FpswareSettings.Instance.Druid.GuardianBerserk)),
                    Spells.Cast("Incarnation", ret =>  CLC.ResultOK(FpswareSettings.Instance.Druid.GuardianIncarnation)),

                    Spells.Cast("Maul", ret => Me.CurrentRage > FpswareSettings.Instance.Druid.MaulRage),
                    Spells.Cast("Faerie Fire", ret => (CT.AuraTimeLeft("Faerie Fire") < 3 && CT.AuraTimeLeft("Faerie Swarm") < 3) && CLC.ResultOK(FpswareSettings.Instance.Druid.GuardianFaerieFire)),
                    Spells.Cast("Mangle", ret => CLC.ResultOK(FpswareSettings.Instance.Druid.GuardianMangle)),
                    Spells.Cast("Lacerate", ret =>  CLC.ResultOK(FpswareSettings.Instance.Druid.Lacerate)),
                    
                    // Different calls for Thrash, both for single target and efficent AoE, no point casting it on adds when they are out of range.
                    Spells.Cast("Thrash", ret => FpswareSettings.Instance.Druid.GuardianThrash.Contains("always")),
                    Spells.Cast("Thrash", ret => FpswareSettings.Instance.Druid.GuardianThrash.Contains("adds") && CT.HostileUnitsNearTarget(8f).Count() >= 2),
                    Spells.Cast("Swipe", ret => CLC.ResultOK(FpswareSettings.Instance.Druid.GuardianSwipe) && CT.HostileUnitsNearTarget(8f).Count() >= 2 && Timers.SpellOkToCast("Guardian Swipe", 3000)),
                    

                    Movement.MoveToMelee(true)
                    );
            }
        }

        public static Composite PullBehavior_Guardian
        {
            get
            {
                return new PrioritySelector(

                    Spells.WaitForCast(),
                    Misc.EnsureTarget(),
                    new Sequence(ret => Me.Mounted, new Action(ret => Spells.Dismount("Pulling")), new Action(ctx => RunStatus.Failure)),
                    Movement.MoveToLOS(),
                    Movement.FaceTarget(),

                    Spells.BuffSelf("Bear Form", ret => Me.Shapeshift != ShapeshiftForm.Bear),

                    Spells.Cast("Wild Charge", ret => CT.Distance > 8 && CT.Distance < 25 && CLC.ResultOK(FpswareSettings.Instance.Druid.GuardianWildCharge)),
                    Spells.Cast("Enrage", ret => Me.CurrentRage < FpswareSettings.Instance.Druid.EnrageRage),
                    Spells.Cast("Stampeding Roar", ret => CT.Distance2D > 40 * 40),
                    Spells.Cast("Faerie Fire", ret => !CT.HasMyAura("Faerie Fire") && !Me.HasAura("Prowl")),
                    Spells.Cast("Mangle", ret => true),

                    Movement.MoveToMelee(true)
                    );
            }
        }
    }
}
